<template>
  <div class="app-container">
    <el-button type="primary" size="small" @click="handleAddTop">添加一级菜单</el-button>
    <p> </p>
    <el-table :data="tableData" style="width: 100%" row-key="name" border >
      <el-table-column prop="label" label="菜单名称"></el-table-column>
      <el-table-column prop="url" label="菜单路径"></el-table-column>
      <el-table-column prop="name" label="关键字"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="success" size="small" @click="handleAdd(scope.row)">添加</el-button>
          <el-button type="primary" size="small" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDelete(scope.row, scope.$index)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="菜单详情" :visible.sync="dialogVisible" width="500px" :before-close="handleClose">
      <el-form ref="form" :model="dialogForm" :rules="rules" label-width="80px">
        <el-form-item label="菜单名称" prop="label">
          <el-input v-model="dialogForm.label" placeholder="请输入菜单显示名称"></el-input>
        </el-form-item>
        <el-form-item label="菜单路径" prop="url">
          <el-input v-model="dialogForm.url" placeholder="/order/list"></el-input>
        </el-form-item>
        <el-form-item label="关键字" prop="name">
          <el-input v-model="dialogForm.name" placeholder="请输入菜单唯一ID"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getMenu, editMenu } from '@/api/menu'
import { findParentNode } from '@/utils'
export default {

  data() {
    return {
      editType: '',
      editRow: {},
      dialogForm: {
        name: '',
        url: '',
        label: ''
      },
      dialogVisible: false,
      tableData: [],
      rules: {
        label: [{ required: true, message: '请输入菜单名称', trigger: 'blur' }],
        url: [{ required: true, message: '请输入菜单路径', trigger: 'blur' }],
        name: [{ required: true, message: '请输入菜单关键字', trigger: 'blur' }]
      }
    }
  },
  mounted() {
    this.getTree()
  },
  methods: {
    async getTree() {
      const res = await getMenu()
      if (res.code === 0) {
        this.tableData = res.data
      }
    },
    handleEdit(row) {
      this.editType = 'edit'
      this.editRow = row
      this.dialogForm.label = row.label
      this.dialogForm.name = row.name
      this.dialogForm.url = row.url
      this.dialogVisible = true

    },
    handleAdd(row) {
      this.editType = 'add'
      this.editRow = row
      this.dialogForm.label = ''
      this.dialogForm.name = ''
      this.dialogForm.url = ''
      this.dialogVisible = true

    },
    confirm() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          this.submit()
        }
      })
    },
    async submit() {
      const { label, name, url } = this.dialogForm
      const item = { label, name, url }
      if (this.editType === 'edit') {
        // 编辑
        this.editRow.label = label
        this.editRow.name = name
        this.editRow.url = url
      } else if (this.editType === 'add') {
        // 新增
        if (this.editRow.children) {
          this.editRow.children.push(item)
        } else {
          this.editRow.children = [item]
        }
      } else if (this.editType === 'delete') {
        console.log('删除');
      } else if (this.editType === 'addTop') {
        this.tableData.push(item)
      }
      const res = await editMenu({ tree: this.tableData })
      if (res.code === 0) {
        this.dialogVisible = false
        this.$message({
          type: 'success',
          message: '修改成功'
        })
        this.handleClose()
      } else {
        this.$message({
          type: 'error',
          message: '修改失败'
        })
      }
      this.getTree()
    },
    handleClose() {
      this.dialogVisible = false
      this.dialogForm.label = ''
      this.dialogForm.name = ''
      this.dialogForm.url = ''
      this.editType = ''
      setTimeout(() => this.$refs.form?.clearValidate(), 500)
    },
    handleDelete(row) {
      this.$confirm('此操作将永久删除该菜单, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.editType = 'delete'
        const parent = findParentNode(this.tableData, row.name)
        if (parent) {
          if (parent.children.legnth === 1) {
            parent.children = undefined
          } else {
            const index = parent.children.findIndex(item => item.name === row.name)
            parent.children.splice(index, 1)
          }
        } else {
          // 顶层
          this.tableData = this.tableData.filter(item => item.name !== row.name)
        }
        this.submit()
      }).catch(() => {})
    },
    handleAddTop() {
      this.editType = 'addTop'
      this.dialogForm.label = ''
      this.dialogForm.name = ''
      this.dialogForm.url = ''
      this.dialogVisible = true
    }
  }
}
</script>

